<?php

/**
 * @category   MM
 * @package    MM_Service
 */
class MM_Service_Admin_Guests extends Mediad2_Service_Abstract
{
	/**
	 * 初始化
	 */
	public function init()
	{
		$this->setMapper('MM_Mapper_Guests');
		$this->setModel('MM_Model_Guests');
	}
	
	/**
	 * 删除嘉宾
	 * @param int $uid
	 * @param int $gid
	 * @return Mediad2_Model_Result
	 */
	public function delGuests($uid, $gid)
	{
		$where = "uid = $uid AND gid = $gid";
		return $this->deleteBy($where);
	}
	
	/**
	 * 更新嘉宾信息
	 * @param int $gid
	 * @param array $params
	 * @return Mediad2_Model_Result
	 */
	public function updateGuests($gid, array $params)
	{
		try {
			//获取指定id的guests
			$result = $this->find($gid);
			if ($result->getCode()==$result::CODE_FAILURE) return $result;
	
			//更新嘉宾信息时,同步更新该嘉宾所有参会的event_guest里面的内容
			
			//事务处理
			$db1 = Zend_Registry::get('db1');
			$db1->beginTransaction();
			
			//vincent start ----------------------------------------
			
			try {
				
				//[1]更新嘉宾信息
				
				//封装model
				$model = $result->getData();
				
				if (isset($params['name'])) $model->setName($params['name']);
				if (isset($params['pinyin'])) $model->setPinyin($params['pinyin']);
				if (isset($params['company'])) $model->setCompany($params['company']);
				if (isset($params['position'])) $model->setPosition($params['position']);
				if (isset($params['email'])) $model->setEmail($params['email']);
				if (isset($params['qq'])) $model->setQq($params['qq']);
				if (isset($params['mobile'])) $model->setMobile($params['mobile']);
				if (isset($params['website'])) $model->setWebsite($params['website']);
				if (isset($params['desc'])) $model->setDesc($params['desc']);
				if (isset($params['sinaweibo'])) $model->setSinaweibo($params['sinaweibo']);
				if (isset($params['sinanickname'])) $model->setSinanickname($params['sinanickname']);
				if (isset($params['qqweibo'])) $model->setQqweibo($params['qqweibo']);
				if (isset($params['qqnickname'])) $model->setQqnickname($params['qqnickname']);
				if (isset($params['avatar'])) $model->setAvatar($params['avatar']);
				
				$this->result =  $this->update($model);//db Action
				
				if($this->result->getCode()==$result::CODE_SUCCESS){					
				//[2]同步更新参会嘉宾信息
				
					//所有活动嘉宾信息列更新
					$eventguest = new MM_Service_Admin_EventGuests();
					$row = $model->toArray();
					foreach ($row as $key => $value) {
						if($key=="uid")
							unset($row['uid']);
					}
					$where = sprintf("gid = '%s'",$gid);
					$mapper = $eventguest->getMapper();
					$mapper->__updateSQL($row, $where);//db Action
				}				
			
				//提交事务
				$db1->commit();
			
				return $this->result;
			
			} catch (Zend_Exception $e) {
			
				//回滚事务
				$db1->rollback();
			
				//出错处理
				$this->handleException($e);
			
				//返回结果--失败
				return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE,"update gueset fn fail");
			}
			
			//vincent end-------------------------------------------
			
			
			//old method below by ty
			/* //封装model
			$model = $result->getData();

			if (isset($params['name'])) $model->setName($params['name']);
			if (isset($params['pinyin'])) $model->setPinyin($params['pinyin']);
			if (isset($params['company'])) $model->setCompany($params['company']);
			if (isset($params['position'])) $model->setPosition($params['position']);
			if (isset($params['email'])) $model->setEmail($params['email']);
			if (isset($params['qq'])) $model->setQq($params['qq']);
			if (isset($params['mobile'])) $model->setMobile($params['mobile']);
			if (isset($params['website'])) $model->setWebsite($params['website']);
			if (isset($params['desc'])) $model->setDesc($params['desc']);
			if (isset($params['sinaweibo'])) $model->setSinaweibo($params['sinaweibo']);
			if (isset($params['sinanickname'])) $model->setSinanickname($params['sinanickname']);
			if (isset($params['qqweibo'])) $model->setQqweibo($params['qqweibo']);
			if (isset($params['qqnickname'])) $model->setQqnickname($params['qqnickname']);
			if (isset($params['avatar'])) $model->setAvatar($params['avatar']);
				
			return $this->update($model); */
	
		} catch (Zend_Exception $e) {
			$this->handleException($e); //出错处理
			return $this->returnResult(Mediad2_Model_Result::CODE_FAILURE); //返回结果--失败
		}
	}
	
}